﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAO;


namespace ProyectoCine.AreaPrivada
{
    public partial class InformePeliculas : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["Orden"] = "";
                cargarComboCine();
                cargarComboPelicula();
                DateTime fecha = new DateTime(2000, 01, 01);
                txtFechaDesde_CalendarExtender.SelectedDate = fecha;
                txtFechaHasta_CalendarExtender.SelectedDate = DateTime.Today;

            }
        }
        protected void cargarComboCine()
        {
            ddlCine.DataSource = PeliculaDAO.cargarComboCine();
            ddlCine.DataTextField = "nombre";
            ddlCine.DataValueField = "id_Cine";
            ddlCine.DataBind();
            ddlCine.Items.Insert(0, new ListItem("Todos los Cines"));
        }
        protected void cargarComboPelicula()
        {
            ddlPelicula.DataSource = PeliculaDAO.cargarCombo();
            ddlPelicula.DataTextField = "nombre";
            ddlPelicula.DataValueField = "id_Pelicula";
            ddlPelicula.DataBind();
            ddlPelicula.Items.Insert(0, new ListItem("Todas las Peliculas"));
        }

        protected void btnGenerar_Click(object sender, EventArgs e)
        {
            cargarGrilla();
        }

        public void cargarGrilla()
        {
            int? pelicula = null;
            if (ddlPelicula.SelectedIndex != 0)
                pelicula =int .Parse( ddlPelicula.SelectedValue);
            int? cine = null;
            if (ddlCine.SelectedIndex != 0)
                cine = int.Parse(ddlCine.SelectedValue);
            gvInforme.DataKeyNames = new string[] { "nombre" };
            gvInforme.DataSource = InformeDAO.cargarGrillaByFiltroPelicula(pelicula, cine, txtFechaDesde.Text, txtFechaHasta.Text, ViewState["Orden"].ToString());
            gvInforme.DataBind();
        }

        protected void gvInforme_Sorting(object sender, GridViewSortEventArgs e)
        {
            ViewState["Orden"] = e.SortExpression;
            cargarGrilla();
        }

        protected void gvInforme_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvInforme.PageIndex = e.NewPageIndex;
            cargarGrilla();
        }
    }
}